﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.WXshopBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;


namespace Repository.WXshopBuiness
{
    public static class PromotionRep
    {
        public static Promotions GetPromotionsByUnidName(this IRepositoryAsync<Promotions> repository, string unidName)
        {
            Expression<Func<Promotions, bool>> expression = i => 1 == 1;
            if (string.IsNullOrEmpty(unidName))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.PromotionUnidName == unidName);
            }
            return repository.Query().Where(expression).Select().FirstOrDefault();
        }

        public static Promotions GetById(this IRepositoryAsync<Promotions> repository, string PromotionId)
        {
            Expression<Func<Promotions, bool>> expression = i => 1 == 1;
            if (string.IsNullOrEmpty(PromotionId))
            {
                return null;
            }
            else
            {
                expression = expression.AndAlso(i => i.PromotionID == PromotionId);
            }
            return repository.Query().Where(expression).Select().FirstOrDefault();
        }
        public static List<Promotions> GetPromotionsList(this IRepositoryAsync<Promotions> repository, string promotionName, DateTime? startTime, DateTime? endTime, string PromotionUnidName,int? disable)
        {
            Expression<Func<Promotions, bool>> expression = i => 1 == 1;
            if (!string.IsNullOrEmpty(promotionName))
            {               
                expression = expression.AndAlso(i => i.PromotionName.Contains(promotionName));
            }
            if (!string.IsNullOrEmpty(PromotionUnidName))
            {
                expression = expression.AndAlso(i => i.PromotionUnidName.Contains(PromotionUnidName));
            }
            if (startTime.HasValue)
            {
                expression = expression.AndAlso(i => i.StartTime>=startTime.Value);
            }
            if (endTime.HasValue)
            {
                expression = expression.AndAlso(i => i.EndTime<=endTime.Value);
            }
            if (disable.HasValue)
            {
                expression = expression.AndAlso(i => i.Disable ==  disable.Value);
            }
            return repository.Query().Where(expression).Select().ToList();
        }
    }
}
